package cn.echoedu.model;

import java.sql.SQLException;
import java.util.List;

import cn.echoedu.daoconstants.TableName;
import cn.echoedu.entity.ReceiveSchoolNotify;
import cn.echoedu.entity.SchoolNotify;
import cn.echoedu.entity.receivecommand.TeacherAddNotifyReceive;
import cn.echoedu.util.L;
import cn.echoedu.util.StringUtil;

public class SchoolNotifyModel extends BaseModel{
	private final String T = "SchoolNotifyModel********>>";
	public int addSchoolNotifyContent(SchoolNotify schoolnotify) {
		// TODO Auto-generated method stub

		return   putInfoGetPrimaryKey(schoolnotify);
	}

	private int putInfoGetPrimaryKey(SchoolNotify schoolnotify) {
		// TODO Auto-generated method stub
		try{
			return getDAOInsert().putInfoGetPrimaryKey(getConnection(), TableName.SCHOOL_NOTIFY_TABLE, 
					schoolnotify,true);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

	public boolean deleteNotifyById(int schoolnotifykey) {
		// TODO Auto-generated method stub
		SchoolNotify schoolnotify = new SchoolNotify();
		schoolnotify.setSchoolnotifyid(schoolnotifykey);
		return deleteFrom(schoolnotify);
	}

	private boolean deleteFrom(SchoolNotify schoolnotify) {
		// TODO Auto-generated method stub
		try {
			return getDAODelete().deleteFrom(getConnection(),TableName.SCHOOL_NOTIFY_TABLE,
					schoolnotify);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public List<SchoolNotify> queryByReceiveSchoolNotifys(
			List<ReceiveSchoolNotify> receiveschoolnotifylist) {
		// TODO Auto-generated method stub
		if(receiveschoolnotifylist==null||receiveschoolnotifylist.size()==0){
			return null;
		}
		String sql = getReceiveSchoolNotifyQuerySql(receiveschoolnotifylist);
		L.p(T+sql);
		return getPojoForSql(sql);
	}
	private List<SchoolNotify> getPojoForSql(String sql){

		try {
			return getDAORead().getPojoForSql(getConnection(), TableName.SCHOOL_NOTIFY_TABLE,
					sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	private String getReceiveSchoolNotifyQuerySql(List<ReceiveSchoolNotify> receiveschoolnotifylist){
		StringBuilder sqlstring = new StringBuilder();
		sqlstring.append("select * from schoolnotify where schoolnotifyid in");
		sqlstring.append(StringUtil.getBeforeTheBsrackets());
		for(int i=0,len=receiveschoolnotifylist.size();i<len;i++){

			//			L.p(T+i+"III"+receiveschoolnotifylist.
			//					get(i).getSchoolnotify().getSchoolnotifyid());
			L.p(T+i+"receiveschoolnotifylist"+receiveschoolnotifylist);
			L.p(T+i+"get(i)"+receiveschoolnotifylist.get(i));
			L.p(T+i+"get(i).getSchoolnotify()"+
					receiveschoolnotifylist.get(i).getSchoolnotify());
			L.p(T+i+"get(i).getSchoolnotify().getSchoolnotifyid()"+
					receiveschoolnotifylist.get(i).getSchoolnotify().getSchoolnotifyid());
			sqlstring.append(StringUtil.varToSqlObject(receiveschoolnotifylist.
					get(i).getSchoolnotify().getSchoolnotifyid()));
			if(i<len-1){
				sqlstring.append(StringUtil.getComma());
			}
		}
		sqlstring.append(StringUtil.getAfterTheBrackets());
		return sqlstring.toString();
	}

	public SchoolNotify queryBySchoolNotifyId(int schoolnotifyid) {
		// TODO Auto-generated method stub
		SchoolNotify schoolnotify = null;
		try {
			schoolnotify = getDAORead().getPojoForPrimarKey(getConnection(),
					TableName.SCHOOL_NOTIFY_TABLE, schoolnotifyid+"");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return schoolnotify;
	}

	public int getNotifyCount(TeacherAddNotifyReceive teacheraddnotifyreceive) {
		// TODO Auto-generated method stub
		int num = 0;
		try {
			num = getDAORead().getCount(getConnection(),TableName.SCHOOL_NOTIFY_TABLE);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	public int getMaxIdOfNotifyOfDepartment(TeacherAddNotifyReceive teacheraddnotifyreceive) {
		// TODO Auto-generated method stub
		int maxid = 0;
		try {
			maxid = getDAORead().getMaxId(getConnection(),TableName.SCHOOL_NOTIFY_TABLE
					,"schoolnotifyid","departmentcode",
					teacheraddnotifyreceive.getDepartmentcode()+"","");
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return maxid;
	}

	public int getMaxIdOfNotify(TeacherAddNotifyReceive teacheraddnotifyreceive) {
		// TODO Auto-generated method stub
		int maxid = 0;
		try {
			maxid = getDAORead().getMaxId(getConnection(),TableName.SCHOOL_NOTIFY_TABLE
					,"schoolnotifyid",null,null,"");
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return maxid;
	}

	public boolean addSchoolNotifyKeyContent(SchoolNotify schoolnotify) {
		// TODO Auto-generated method stub
		try {
			return getDAOInsert().putInto(getConnection(), TableName.SCHOOL_NOTIFY_TABLE,
					schoolnotify,false);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;

	}

	public boolean updateSchoolNotify(SchoolNotify oldschoolnotify,
			SchoolNotify newschoolnotify) {
		// TODO Auto-generated method stub
		try {
			return	getDAOUpdate().updateInfo(getConnection(),TableName.SCHOOL_NOTIFY_TABLE,
					oldschoolnotify,newschoolnotify);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
